﻿@using System.Data;
@using DTcms.Common;
@{
    ViewBag.Title = "生成静态页面";
    List<SelectListItem> siteList = ViewBag.siteListItems;
    DTcms.Model.siteconfig sysConfig = ViewBag.siteConfig;
    DTcms.BLL.article_category bll = new DTcms.BLL.article_category();
}
@model IEnumerable<DTcms.Model.site_channel>
<form id="form1" class="layui-form">
    <!--导航栏-->
    <div class="pageheader">
        <div class="page-bar">
            <ul class="page-breadcrumb">
                <li><a href="../channel/site_list" class="back"><i class="fa fa-mail-reply-all"></i> 返回列表页</a></li>
                <li><a href="javascript:;"><i class="iconfont icon-home1"></i> 首页</a> </li>
                <li><a href="javascript:;">界面管理</a> </li>
                <li><a href="javascript:;">生成静态</a> </li>
            </ul>
        </div>
    </div>
    <!--/导航栏-->
    <!--列表-->
    <!--工具栏-->
    <div class="layui-fluid">
        <div class="layui-card layui-tab">
            <div class="layui-card-body table-tool-mini">
                <div class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            @foreach (var kv in siteList)
                            {
                                @Html.RadioButton("rblSiteId", kv.Value, (ViewBag.site_id == kv.Value ? true : false), new { title = kv.Text, @lay_filter = "selectChanged" })
                            }
                        </div>
                    </div>
                </div>
                <!--内容-->
                @if (ViewBag.tabContent)
                {
                    <div id="tabContent">
                        <table width="100%" border="0" cellspacing="0" cellpadding="0" class="ltable" id="ltable">
                            <tr>
                                <th align="left" width="120px" style="padding-left:10px;">频道列表</th>
                                <th align="left">操作</th>
                            </tr>
                            <tr>
                                <td valign="middle" align="center">
                                    <b>系统</b>
                                </td>
                                <td>
                                    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="ltable">
                                        <tr>
                                            <th align="right" width="200px" style="padding-right:10px;">生成首页</th>
                                            <th align="left"><a href="javascript:;" url="/admin_ajax/get_builder_urls?lang=@ViewBag.BuildPath&name=&type=index" onclick="builerTip(this);">开始生成</a></th>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            @foreach (var item in Model)
                            {
                                //绑定分类
                                DataTable dt = bll.GetList(0, item.id);
                                List<SelectListItem> List0 = new List<SelectListItem>();
                                List<SelectListItem> List1 = new List<SelectListItem>();
                                List<SelectListItem> List2 = new List<SelectListItem>();

                                List0.Add(new SelectListItem() { Text = "全部", Value = "0" });
                                List1.Add(new SelectListItem() { Text = "全部", Value = "0" });
                                List2.Add(new SelectListItem() { Text = "全部", Value = "0" });
                                foreach (DataRow item0 in dt.Rows)
                                {
                                    string Id = item0["id"].ToString();
                                    int ClassLayer = int.Parse(item0["class_layer"].ToString());
                                    string Title = item0["title"].ToString().Trim();
                                    if (ClassLayer == 1)
                                    {
                                        List0.Add(new SelectListItem() { Text = Title, Value = Id });
                                        List1.Add(new SelectListItem() { Text = Title, Value = Id });
                                        List2.Add(new SelectListItem() { Text = Title, Value = Id });
                                    }
                                    else
                                    {
                                        Title = "├ " + Title;
                                        Title = Utils.StringOfChar(ClassLayer - 1, "　") + Title;
                                        List0.Add(new SelectListItem() { Text = Title, Value = Id });
                                        List1.Add(new SelectListItem() { Text = Title, Value = Id });
                                        List2.Add(new SelectListItem() { Text = Title, Value = Id });
                                    }
                                }
                                <tr>
                                    <td valign="middle" align="center">
                                        <b>@item.title</b>
                                    </td>
                                    <td>
                                        <table width="100%" border="0" cellspacing="0" cellpadding="0" class="ltable">
                                            <tr>
                                                <th align="right" width="200px" style="padding-right:10px;">生成首页</th>
                                                <th align="left"><a href="javascript:;" url="/admin_ajax/get_builder_urls?lang=@ViewBag.BuildPath&name=@item.name&type=indexlist" onclick="builerTip(this);">开始生成</a></th>
                                            </tr>
                                            <tr>
                                                <th align="right" width="200px" style="padding-right:10px;">生成栏目</th>
                                                <th align="left">
                                                    @Html.DropDownList("dropDownList0", new SelectList(List0, "Value", "Text", 0))
                                                    <a href="javascript:;" url="/admin_ajax/get_builder_urls?lang=@ViewBag.BuildPath&name=@item.name&type=category&hanld=2" onclick="builerTip2(this);">开始生成</a>
                                                </th>
                                            </tr>
                                            <tr>
                                                <th align="right" width="200px" style="padding-right:10px;">生成分类列表</th>
                                                <th align="left">
                                                    @Html.DropDownList("dropDownList1", new SelectList(List1, "Value", "Text", 0))
                                                    <a href="javascript:;" url="/admin_ajax/get_builder_urls?lang=@ViewBag.BuildPath&name=@item.name&type=list&hanld=3" onclick="builerTip2(this);">开始生成</a>
                                                </th>
                                            </tr>
                                            <tr>
                                                <th align="right" width="200px" style="padding-right:10px;">按分类生成内容</th>
                                                <th align="left">
                                                    @Html.DropDownList("dropDownList2", new SelectList(List2, "Value", "Text", 0))
                                                    <a href="javascript:;" url="/admin_ajax/get_builder_urls?lang=@ViewBag.BuildPath&name=@item.name&type=detail&hanld=4" onclick="builerTip2(this);">开始生成</a>
                                                </th>
                                            </tr>
                                            <tr>
                                                <th align="right" width="200px" style="padding-right:10px;">按日期生成内容</th>
                                                <th align="left">
                                                    @Html.TextBox("txtStart", DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss"), new { @class = "input rule-date-input" }) 到
                                                    @Html.TextBox("txtEnd", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), new { @class = "input rule-date-input", })
                                                    <a href="javascript:;" url="/admin_ajax/get_builder_urls?lang=@ViewBag.BuildPath&name=@item.name&type=detail&hanld=5" onclick="builerTip3(this);">开始生成</a>
                                                </th>
                                            </tr>
                                            <tr>
                                                <th align="right" width="200px" style="padding-right:10px;">生成最新多少条内容</th>
                                                <th align="left">
                                                    @Html.TextBox("txtNum", 100, new { @class = "input small" })
                                                    <a href="javascript:;" url="/admin_ajax/get_builder_urls?lang=@ViewBag.BuildPath&name=@item.name&type=detail&hanld=6" onclick="builerTip4(this);">开始生成</a>
                                                </th>
                                            </tr>
                                            <tr>
                                                <th align="right" width="200px" style="padding-right:10px;">按指定ID生成(多个以逗号隔开)</th>
                                                <th align="left">
                                                    @Html.TextBox("txtId", item.sort_id, new { @class = "input mainval" })
                                                    <a href="javascript:;" url="/admin_ajax/get_builder_urls?lang=@ViewBag.BuildPath&name=@item.name&type=detail&hanld=7" onclick="builerTip5(this);">开始生成</a>
                                                </th>
                                            </tr>
                                            <tr>
                                                <th align="right" width="200px" style="padding-right:10px;">一键生成全部</th>
                                                <th align="left"><a href="javascript:;" url="/admin_ajax/get_builder_urls?lang=@ViewBag.BuildPath&name=@item.name" onclick="builerTip(this);">开始生成</a></th>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            }
                            @if (Model.Count() == 0)
                            {
                                <tr><td align="center" colspan="2">暂无记录</td></tr>
                            }
                        </table>
                    </div>
                }
                <!--内容-->
            </div>
        </div>
    </div>
    <!--/工具栏-->

</form>
@section Scripts{
    <script type="text/javascript">
        //全局变量声明
        var isLock = false; //是否锁定正在执行操作
        var dialogDG; //dialog窗口实例
        layui.use(['form', 'laydate'], function () {
            var form = layui.form, laydate = layui.laydate, $ = layui.jquery;
            $(".rule-date-input").each(function () {
                laydate.render({
                    elem: this
                    , type: 'datetime'
                });
            })
            form.on('radio(selectChanged)', function (data) {
                document.location = "builder_html?site_id=" + data.value;
            });

            //①提示且生成相应的频道
            window.builerTip= function(obj) {
                //检查是否正在执行操作
                if (isLock) {
                    top.layer.alert('上次操作未完成，不可同时执行！', {
                        closeBtn: 0
                    }, function (index) {
                        top.layer.close(index);
                    });
                    return false;
                }
                //提示是否执行
                top.layer.alert('此操作将会消耗大量的资源，确认要继续吗！', {
                    closeBtn: 1
                }, function (index) {
                    top.layer.close(index);
                    getBuilerUrl(obj);
                });
            }

            //方法2
            window.builerTip2= function(obj) {
                var category_id = $(obj).prev("div").find("select").val();
                getBuilerUrl(obj, $(obj).attr("url") + "&lst=" + category_id);
            }

            //方法3
            window.builerTip3= function(obj) {
                var startTime = $(obj).prev("input").prev("input").val(),
                    endTime = $(obj).prev("input").val();
                if (startTime == "" || endTime == "") {
                    layer.alert('生成数据数量不能为空，默认为100！', {
                        closeBtn: 0
                    }, function (index) {
                        layer.close(index);
                    });
                    return false;
                }
                getBuilerUrl(obj, $(obj).attr("url") + "&start=" + startTime + "&end=" + endTime);
            }

            //方法4
            window.builerTip4= function(obj) {
                var count = $(obj).prev("input").val();
                if (count == "") {
                    layer.alert('生成数据数量不能为空，默认为100！', {
                        closeBtn: 0
                    }, function (index) {
                        layer.close(index);
                    });
                    return false;
                }
                getBuilerUrl(obj, $(obj).attr("url") + "&lst=" + count);
            }

            //方法5
            window.builerTip5= function(obj) {
                var num = $(obj).prev("input").val();
                if (num == "") {
                    layer.alert('ID 不能为空，多个ID请以逗号隔开！', {
                        closeBtn: 0
                    }, function (index) {
                        layer.close(index);
                    });
                    return false;
                }
                getBuilerUrl(obj, $(obj).attr("url") + "&lst=" + num);
            }

            //②发送AJAX请求获取生成地址
            function getBuilerUrl(obj, url) {
                //如dialog窗口不存在则创建
                if (!dialogDG) {
                    createDialogObj();
                }
                //重置dialog窗口的值
                dialogDG.find('.title h2').html('正在获取信息...');
                dialogDG.find('.content').html('正在加载，请稍候...');
                isLock = true; //锁定操作
                if (url == null) {
                    url = $(obj).attr("url");
                }
                //发送AJAX请求
                $.ajax({
                    url: url,
                    type: "POST",
                    success: function (data) {
                        if (data == 0) {
                            dialogDG.find('.title h2').html('执行生成处理完毕');
                            dialogDG.find('.content').html('该栏目下没有内容！');
                            isLock = false; //解除锁定
                        }
                        else if (data == -1) {
                            dialogDG.find('.title h2').html('执行请求完毕');
                            dialogDG.find('.content').html('<font color=red>登陆超时！</font>');
                            isLock = false; //解除锁定
                        }
                        else if (data == -2) {
                            dialogDG.find('.title h2').html('执行请求完毕');
                            dialogDG.find('.content').html('<font color=red>您没有操作生成静态的权限！</font>');
                            isLock = false; //解除锁定
                        }
                        else if (data == -3) {
                            dialogDG.find('.title h2').html('执行请求完毕');
                            dialogDG.find('.content').html('<font color=red>您还未开启生成静态功能！<a navid=\"site_config\" class="J_menuItem" href=\"../settings/sys_config" target=\"mainframe\">立即开启</a></font>');
                            isLock = false; //解除锁定
                        }
                        else {
                            var json = eval(data);
                            if (json == "") {
                                dialogDG.find('.title h2').html('执行生成处理完毕');
                                dialogDG.find('.content').html('<font color=red>没有需要生成数据！</font>');
                                isLock = false; //解除锁定
                            }
                            else {
                                execBuilerHtml(json, 0);
                            }
                        }
                    }
                });
            }

            //③迭代执行生成
            function execBuilerHtml(jsonUrl, k) {
                if (jsonUrl[k] != undefined) {
                    $.ajax({
                        url: jsonUrl[k],
                        type: "POST",
                        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                        error: function (data) {
                            getBuilerStatus(jsonUrl, k, "需要生成的静态页面路径有误！");
                        },
                        success: function (data) {
                            if (data != 1 && data != 2 && data != 0) {
                                data = "错误";
                            }
                            getBuilerStatus(jsonUrl, k, data);
                        }
                    });
                } else {
                    getBuilerStatus(jsonUrl, k, "忽略");
                }
            }

            //④返回执行结果及状态
            function getBuilerStatus(jsonUrl, k, msg) {
                if (jsonUrl[k] != undefined) {
                    var fodname = jsonUrl[k].split('&catalogue=');
                    var fname = jsonUrl[k].split('&html_filename=');
                    fname = fname[1].split('&catalogue=');
                    fname[0] = unescape(fname[0]);
                    var finame = !fodname[1] ? fname[0] + '.html' : fodname[1];

                    var spanTxt = msg == 0 ? '<span class="suc">成功</span>' : '<span class="error">失败</span>';
                    var linkTxt = spanTxt + '<a href="@sysConfig.webpath' + finame + '" target="_blank">/' + finame + '</a>';

                    dialogDG.find('.title h2').html('已完成页面生成' + '[' + jsonUrl.length + '/' + (k + 1) + ']');
                    if (dialogDG.find('.content .list').length == 0) {
                        dialogDG.find('.content').html('');
                    }
                    dialogDG.find('.content').append('<div class="list">' + linkTxt + '</div>');
                }
                if (k == jsonUrl.length - 1) {
                    isLock = false; //解除锁定
                    //完成提示
                    layer.msg("页面全部生成完毕！", { time: 2000, icon: 1 });
                } else {
                    k++;
                    execBuilerHtml(jsonUrl, k);
                }
            }

            //创建dialog窗口
            function createDialogObj() {
                dialogDG = $('<div id="buildDialog" class="builder-box">'
                           + '<div class="title">'
                           + '<a class="close" onclick="closeDialogObj();"></a>'
                           + '<h2>请稍候...</h2></div>'
                           + '<div class="content"></div></div>');
                dialogDG.appendTo($('body'));
            }

            //删除dialog窗口
            window.closeDialogObj= function() {
                dialogDG.remove();
                dialogDG = null;
            }
        });
    </script>
}